﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FastPoster
{
    #region USING
    using DevComponents.DotNetBar;
    using FastPoster.MainForms;
    using FastPoster.Helpers;
    using System.Xml;
    #endregion

    public partial class MainForm : Office2007Form
    {
        #region VARIABLES
        private FrmDienDan                  frmDienDan;
        private FrmDangTin                  frmDangTin;
        private FrmKetQuaDangTin            frmKetQuaDangTin;
        private FrmXemCacBaiVietDaDang      frmXemCacBaiVietDaDang;
        private FrmTestDienDan              frmTestDienDan;
        private bool                        _Trial; 
        #endregion

        public void InitForms()
        {
            // Dien dan
            frmDienDan = new FrmDienDan();
            frmDienDan.TopLevel = false;
            frmDienDan.FormBorderStyle = FormBorderStyle.None;
            frmDienDan.Dock = DockStyle.Fill;

            // Dang tin
            frmDangTin = new FrmDangTin();
            frmDangTin.TopLevel = false;
            frmDangTin.FormBorderStyle = FormBorderStyle.None;
            frmDangTin.Dock = DockStyle.Fill;
            frmDangTin.Handler += RetrieveBoxList;

            // Ket qua dang tin
            frmKetQuaDangTin = new FrmKetQuaDangTin();
            frmKetQuaDangTin.TopLevel = false;
            frmKetQuaDangTin.FormBorderStyle = FormBorderStyle.None;
            frmKetQuaDangTin.Dock = DockStyle.Fill;

            // Xem cac bai viet da dang 
            frmXemCacBaiVietDaDang = new FrmXemCacBaiVietDaDang();
            frmXemCacBaiVietDaDang.TopLevel = false;
            frmXemCacBaiVietDaDang.FormBorderStyle = FormBorderStyle.None;
            frmXemCacBaiVietDaDang.Dock = DockStyle.Fill;

            // Test dien dan
            frmTestDienDan = new FrmTestDienDan();
            frmTestDienDan.TopLevel = false;
            frmTestDienDan.FormBorderStyle = FormBorderStyle.None;
            frmTestDienDan.Dock = DockStyle.Fill;
        }

        public MainForm(bool isTrial)
        {
            InitializeComponent();

            _Trial = !isTrial;
        }

        private void menDanhMuc_Click(object sender, EventArgs e)
        {
            FrmDanhMuc frm = new FrmDanhMuc();
            frm.Handler += ReloadDanhMuc;
            frm.ShowDialog();
        }

        private void ttbtnDanhMuc_Click(object sender, EventArgs e)
        {
            FrmDanhMuc frm = new FrmDanhMuc();
            frm.Handler += ReloadDanhMuc;
            frm.ShowDialog();
        }

        private void ReloadDanhMuc(object sender, EventArgs e)
        {
            frmDienDan.LoadBoxLinks();
            frmDangTin.LoadDanhMucs();
        }

        public void ActiveForms(Form frm)
        {
            pnlMain.Controls.Clear();
            pnlMain.Controls.Add(frm);
            frm.Show();
        }

        public void GenerateListTuKhoa()
        {
            try
            {
                GlobalVariables.Logger.Info("Generate list keywords: started");
                XmlDocument doc = new XmlDocument();
                doc.Load(Application.StartupPath + "\\Imports\\seo.xml");

                XmlElement root = doc.DocumentElement;

                XmlNode keywords = root.ChildNodes[0];
                XmlNode urls = root.ChildNodes[1];

                GlobalVariables.LstTuKhoa.Clear();

                foreach (XmlNode childNode in keywords.ChildNodes)
                {
                    GlobalVariables.LstTuKhoa.Add(childNode.InnerText);
                }
                GlobalVariables.Logger.Info("Generate list keywords: done");
            }
            catch(Exception ex)
            {
                if(ex.InnerException != null)
                    GlobalVariables.Logger.Info("Generate list keywords: failed, error: " + ex.InnerException.Message);
                else
                    GlobalVariables.Logger.Info("Generate list keywords: failed, error: " + ex.Message);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var databaseName = "";
            if (XMLDataProvider.LoadDBInfo(Application.StartupPath + "\\Configs\\Config.xml", ref databaseName))
            {
                GlobalVariables.g_strPath = Application.StartupPath + "\\DataBase\\" + databaseName;
                InitForms();
                GenerateListTuKhoa();
            }
            else
            {
                MessageBoxEx.Show("Vui lòng kiểm tra lại file cấu hình", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            // Delete trash file
            DeleteTrashFiles();
        }

        public void DeleteTrashFiles()
        {
            try
            {
                var path = Application.StartupPath + "\\DataBase\\trash.xml";

                if (System.IO.File.Exists(path))
                {
                    try
                    {
                        System.IO.File.Delete(path);
                    }
                    catch (System.IO.IOException e)
                    {
                        Console.WriteLine(e.Message);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void ttbtnDangTin_Click(object sender, EventArgs e)
        {
            ActiveForms(frmDangTin);
        }

        private void ttbtnForum_Click(object sender, EventArgs e)
        {
            ActiveForms(frmDienDan);
            frmDienDan.ClearSelection();
        }

        private void ttbtnKetQuaDangTin_Click(object sender, EventArgs e)
        {
            ActiveForms(frmKetQuaDangTin);
        }

        private void RetrieveBoxList(object sender, EventArgs e)
        {
            // Disable btnDangTin
            frmDangTin.EnableBtnDangTin(false);

            var eventType = (TransferBoxListEvent)e;
            
            // Init frmKetQuaDangTin
            frmKetQuaDangTin.LstPoster = eventType.Data;
            frmKetQuaDangTin.IDM = eventType.IDM;
            frmKetQuaDangTin.Subj = eventType.Subj;
            frmKetQuaDangTin.Mess = eventType.Mess;
            
            frmKetQuaDangTin.InitForm();
            frmKetQuaDangTin.Handler += PostingComplete;
            frmKetQuaDangTin.StartPost();
            
            // Active this form
            ActiveForms(frmKetQuaDangTin);
            frmKetQuaDangTin.ClearSelection();
        }

        private void PostingComplete(object sender, EventArgs e)
        {
            frmDangTin.EnableBtnDangTin(true);
        }

        private void ttbtnBaiVietDaDang_Click(object sender, EventArgs e)
        {
            ActiveForms(frmXemCacBaiVietDaDang);
        }

        private void menDienDan_Click(object sender, EventArgs e)
        {
            ActiveForms(frmDienDan);
            frmDienDan.ClearSelection();
        }

        private void menThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menDangTin_Click(object sender, EventArgs e)
        {
            ActiveForms(frmDangTin);
        }

        private void menKetQuaDangTin_Click(object sender, EventArgs e)
        {
            ActiveForms(frmKetQuaDangTin);
        }

        private void menXemBaiVietDaDang_Click(object sender, EventArgs e)
        {
            ActiveForms(frmXemCacBaiVietDaDang);
        }

        private void menThongTinLienHe_Click(object sender, EventArgs e)
        {
            FrmThongTinLienHe frm = new FrmThongTinLienHe();
            frm.ShowDialog();
        }

        private void ttbtnTestDienDan_Click(object sender, EventArgs e)
        {
            ActiveForms(frmTestDienDan);
        }
    }
}
